package model;
import java.util.Random;

import javax.swing.JProgressBar;

import model.BinSetVariables;

public class MonteCarloSimulation {
	
	Double Sum;
	BinSetVariables a = new BinSetVariables();
	JProgressBar barre;
	
	public MonteCarloSimulation(BinSetVariables a, JProgressBar bar){
		this.a = a ;
		this.Sum=0.0;
		this.barre=bar;
		barre.setMaximum(a.getnSimulations());
		
	}
	
	public double MonteCarloStandardOption(BinSetVariables a){
		

		int z=0;
		Double St=0.0;
		
		Random random = new Random();
		
		if (a.getCallPutFlag().compareTo("c")==0){
			z=1;
		}
		else if (a.getCallPutFlag().compareTo("p")==0){
			z=-1;
		}
		
		for (int i=0; i< a.getnSimulations(); i++){
			
			St = a.getS();
			
			for (int j=0; j< a.getnSteps(); j++){
				
				St = St*Math.exp( a.getDrift() + a.getvSqrdt() * (random.nextGaussian()) );
			}
			barre.setValue(i);
			Sum = Sum + Math.max(z*(St-a.getX()), 0);
			
		}
		
		return Math.exp(-a.getR()*a.getT()) * (Sum / a.getnSimulations());
	}
}
